<template>
	<!-- 存货编码维护 -->
	<div>
		<v-card>
			<v-card-title>
				<v-container row class="text-subtitle-1 d-flex justify-center  align-left">
					<v-btn class="ml-2 mt-7" small tile color="primary"
						@click="inventoryModel={},editInventoryDialog=true,addOrUpdate=true">
						新增
					</v-btn>

					<v-divider class="mx-4" inset vertical></v-divider>
					<v-row class="mt-2">
						<v-col cols="12" sm="3" md="2">
							<v-text-field label="存货编码" v-model="inventoryQuery.code" dense outlined>
							</v-text-field>
						</v-col>
						<v-col cols="12" sm="3" md="2">
							<v-text-field label="存货名称" v-model="inventoryQuery.name" dense outlined>
							</v-text-field>
						</v-col>

						<v-col cols="12" sm="3" md="2">
							<v-menu v-model="invClassTreeModel.dialog" :close-on-content-click="false" :nudge-right="40"
								transition="scale-transition" offset-y min-width="auto">
								<template v-slot:activator="{ on, attrs }">
									<v-text-field required v-model="inventoryQuery.sort_name" label="选择存货分类" readonly
										v-bind="attrs" v-on="on" outlined dense>
									</v-text-field>
								</template>
								<invclasstreecomment :invClassTreeModel="invClassTreeModel"
									:callBack="setInvClassCallBack">
								</invclasstreecomment>
							</v-menu>
							<!-- 		
							<v-combobox v-model="inventoryQuery.sort_code" :items="inventoryClassItems" label="存货分类"
								:return-object="false" dense outlined chips>
							</v-combobox> -->
						</v-col>

						<!-- ml-n16 前面加n是间距 -->
						<!-- class="ml-n16 mx-auto" -->
						<v-col cols="12" sm="2" md="2">
							<v-btn depressed color="primary" @click="loadPages">查询</v-btn>
							<v-btn class="ml-3" depressed color="primary" @click="refreshLoadPages">
								刷新</v-btn>
						</v-col>
					</v-row>
				</v-container>
			</v-card-title>
			<v-card-text>
				<v-data-table :headers="inventoryHeaderList" :items="inventoryDataList" small fixed-header
					item-key="cinvcode" dense show-select class="text-no-wrap" loading-text="正在加载请稍等......"
					:loading="inventoryLoading" :options.sync="inventoryOptions" :footer-props="vuetifyDefaultPage"
					:server-items-length="inventoryTotalCount">
					<!-- 
					<template v-slot:item.cinvcname="{ item }">
						<a>{{getInvCinvname(item.cinvccode)}}</a>
					</template> -->
					<template v-slot:item.unitname="{ item }">
						<a>{{getDataListUnit(item.ccomunitcode)}}</a>
					</template>
					<template v-slot:item.bself="{ item }">
						<a>{{item.bself?'是':'否'}}</a>
					</template>
					<template v-slot:item.bPurchase="{ item }">
						<a>{{item.bPurchase?'是':'否'}}</a>
					</template>
					<template v-slot:item.bproducing="{ item }">
						<a>{{item.bproducing?'是':'否'}}</a>
					</template>
					<template v-slot:item.actions="{ item }">
						<div>
							<v-btn class="ml-2" small tile color="primary"
								@click="editInventoryDialog=true,addOrUpdate=false,inventoryModel=item,setSelectUnit(item)">
								编辑
							</v-btn>

						<!-- 	<v-btn class="ml-2" small tile color="red" @click="deleteInventory(item)">
								删除
							</v-btn> -->
						</div>
					</template>

				</v-data-table>

			</v-card-text>
		</v-card>


		<v-dialog v-model="editInventoryDialog" width="700">
			<v-card>
				<v-card-title>编辑存货</v-card-title>
				<v-card-text>
					<v-text-field v-model="inventoryModel.cinvcode" :disabled="!addOrUpdate" label="存货代码" outlined
						dense>
					</v-text-field>
					<v-text-field v-model="inventoryModel.cinvname" label="存货名称" outlined dense></v-text-field>
					<v-row>
						<v-col cols="12" md="9">
							<v-text-field :style="this.temp.code" v-model="inventoryModel.cinvcname" disabled label="存货所属分类"
								outlined dense>
							</v-text-field>
						</v-col>
						<v-col cols="12" md="3">
							<v-btn class="ml-2" :disabled="!addOrUpdate" small tile color="primary" 
								@click="editInvClassTreeModel.dialog=true">
								选择存货分类
							</v-btn>
						</v-col>
					</v-row>
					<v-combobox v-model="inventoryModel.unit" :items="unitItems" label="单位" outlined chips dense>
					</v-combobox>
					<v-text-field v-model="inventoryModel.cinvstd" :disabled="!addOrUpdate" label="规格型号" outlined>
					</v-text-field>
					<v-checkbox v-model="inventoryModel.bsale" label="是否内销" outlined dense></v-checkbox>
					<v-checkbox v-model="inventoryModel.bPurchase" label="是否采购" outlined dense></v-checkbox>
					<v-checkbox v-model="inventoryModel.bself" label="是否自制" outlined dense></v-checkbox>
					<v-checkbox v-model="inventoryModel.bproxyforeign" label="是否委外" outlined dense></v-checkbox>
					<v-checkbox v-model="inventoryModel.bexpsale" label="是否外销" outlined dense></v-checkbox>
					<v-checkbox v-model="inventoryModel.bproducing" label="是否在制" outlined dense></v-checkbox>
					<v-checkbox v-model="inventoryModel.bcomsume" label="是否生产耗用" outlined dense></v-checkbox>
				</v-card-text>
				<v-card-actions>
					<v-spacer></v-spacer>
					<v-btn color="success" class="mr-4" text @click="editInventorySubmit">确认</v-btn>
					<v-btn color="warning" class="mr-4" text @click="close">取消</v-btn>
				</v-card-actions>
			</v-card>
		</v-dialog>
		<!-- 编辑存货的时候选择的存货分类对话框 -->
		<invclasstreecomment :invClassTreeModel="editInvClassTreeModel" :callBack="setEditInvClassCallBack">
		</invclasstreecomment>

	</div>
</template>

<script>
	import InvClassTreeComment from '../components/InvClassTree.vue'
	export default {
		name: 'maintain',
		components: {
			invclasstreecomment: InvClassTreeComment
		},
		data: () => ({

			inventoryQuery: {
				code: '',
				name: '',
				sort_code: '',

			},
			inventoryHeaderList: [],
			inventoryDataList: [],
			inventoryTotalCount: 0,
			inventoryLoading: true,
			inventoryOptions: { //页数
				page: 1,
				//每页行数
				itemsPerPage: 10
			},
			vuetifyDefaultPage: {
				'items-per-page-text': '每页个数',
				'items-per-page-options': [10, 20, 50, 100]
				// 'items-per-page-all-text': '100'
			},
			editInventoryDialog: false,
			inventoryModel: {},
			unitItems: [],
			inventoryClassItems: [],
			editInventoryClassItems: [],
			//true:add  false:update
			addOrUpdate: true,
			invClassTreeModel: {
				dialog: false,
				getTreeUrl: `PDM/PDMMaintain/GetInvclassTree?iinvcgrade=`,
				label: '选择存货分类'
			},
			editInvClassTreeModel: {
				dialog: false,
				getTreeUrl: `PDM/PDMMaintain/GetInvclassTree?iinvcgrade=`,
				label: '选择所属存货分类'
			},
			temp: {}
		}),
		watch: {
			inventoryOptions: {
				handler() {
					this.loadPages()
				},
				deep: true
			},
		},
		created() {
			// this.loadInventoryClass()
			this.loadUnits()
			this.getProjectHeaderList()
		},
		methods: {
			//查询时选择的存货分类
			setInvClassCallBack(invClass) {
				this.invClassTreeModel.dialog = false
				this.inventoryQuery.sort_name = invClass.name
				this.inventoryQuery.sort_code = invClass.code
			},
			//编辑存货时选择的存货分类
			setEditInvClassCallBack(invClass) {
				this.editInvClassTreeModel.dialog = false
				this.temp = invClass
				this.inventoryModel.cinvcname=invClass.name
				this.inventoryModel.cinvccode = invClass.code
			
			},
			setSelectUnit(item) {
				var units = this.unitItems.filter(r => r.unitcode == item.ccomunitcode)
				if (units != null && units.length > 0)
					this.inventoryModel.unit = units[0]
			},
			getDataListUnit(unitcode) {
				var items = this.unitItems.filter(r => r.unitcode == unitcode)
				if (items != null && items.length > 0) {
					return items[0].text
				}
			},
			//根据存货大类编码获取大类名称
			getInvCinvname(cinvccode) {
				var items = this.inventoryClassItems.filter(r => r.value == cinvccode)
				if (items != null && items.length > 0) {
					return items[0].text.replace(/=/g, '')
				}
			},
			async loadInventoryClass() {
				var response = await this.$httpRequest({
					url: `PDM/PDMMaintain/loadInventoryClass`,
					method: 'get',
				})
				if (response.status == 1) {
					this.inventoryClassItems = response.data
					this.editInventoryClassItems = this.deepCopy(response.data)
					this.editInventoryClassItems.forEach(r => {
						// if (r.binvcend != "1") {
						// 	r.disabled = true
						// }
					})
				}
			},
			getClass(o) { //判断数据类型
				return Object.prototype.toString.call(o).slice(8, -1);
			},
			deepCopy(obj) {
				var result, oClass = this.getClass(obj);

				if (oClass == "Object") result = {}; //判断传入的如果是对象，继续遍历
				else if (oClass == "Array") result = []; //判断传入的如果是数组，继续遍历
				else return obj; //如果是基本数据类型就直接返回

				for (var i in obj) {
					var copy = obj[i];

					if (this.getClass(copy) == "Object") result[i] = this.deepCopy(
						copy); //递归方法 ，如果对象继续变量obj[i],下一级还是对象，就obj[i][i]
					else if (this.getClass(copy) == "Array") result[i] = this.deepCopy(
						copy); //递归方法 ，如果对象继续数组obj[i],下一级还是数组，就obj[i][i]
					else result[i] = copy; //基本数据类型则赋值给属性
				}

				return result;
			},

			async loadUnits() {
				var response = await this.$httpRequest({
					url: `PDM/PDMMaintain/loadUnits`,
					method: 'get',
				})
				if (response.status == 1) {
					this.unitItems = response.data
				}
			},


			async loadPages() {
				this.inventoryLoading = true
				var response = await this.$httpRequest({
					url: `PDM/PDMMaintain/LoadPage?
						code=${this.inventoryQuery.code}&
						name=${this.inventoryQuery.name}&
						CInvCode=${this.inventoryQuery.sort_code}&
						pageNumber=${this.inventoryOptions.page}&
						pageSize=${this.inventoryOptions.itemsPerPage}`,
					method: 'get',
				})
				if (response.status == 1) {
					this.inventoryDataList = response.data.result
					this.inventoryTotalCount = response.data.pagination.totalRecords
					this.inventoryLoading = false
				} else {
					this.$message.error(response.info)
				}
			},
			async refreshLoadPages() {
				this.inventoryQuery.code = ''
				this.inventoryQuery.name = ''
				this.inventoryQuery.sort_code = ''
				this.inventoryQuery.sort_name = ''
				await this.loadPages()
			},

			async editInventorySubmit() {
				var url = ''

				if (this.addOrUpdate) {
					url = 'PDM/PDMMaintain/AddInventory'
				} else {
					url = 'PDM/PDMMaintain/UpdateInventory'
				}

				if (this.inventoryModel.unit != null) {
					this.inventoryModel.cgroupcode = this.inventoryModel.unit.unitgroupcode
					this.inventoryModel.ccomunitcode = this.inventoryModel.unit.unitcode
				}

				var response = await this.$httpRequest({
					url: url,
					method: 'post',
					data: this.inventoryModel
				})
				if (response.status == 1) {
					await this.loadPages()
					this.$message.success(response.info)
					this.close()
				} else {
					this.$message.error(response.info)
				}
			},
			async deleteInventory(item) {
				var response = await this.$httpRequest({
					url: `PDM/PDMMaintain/DeleteInventory?Code=${item.cinvcode}`,
					method: 'get',
				})
				if (response.status == 1) {
					this.$message.success(response.info)
					this.loadPages()
				} else {
					this.$message.error(response.info)
				}
			},
			close() {
				this.inventoryModel = {}
				this.editInventoryDialog = false
			},
			getProjectHeaderList() {
				this.inventoryHeaderList = [{
						text: '操作',
						align: 'center',
						value: 'actions',
						filterable: false,
						class: 'fixed'
					},
					{
						text: '存货编码',
						align: 'left',
						value: 'cinvcode',
						filterable: false,
						class: 'fixed'
					},
					{
						text: '存货名称',
						align: 'left',
						value: 'cinvname',
						filterable: false,
						class: 'fixed'
					},
					{
						text: '规格型号',
						align: 'center',
						value: 'cinvstd',
						filterable: false,
						class: 'fixed'
					},
					{
						text: '存货大类编码',
						align: 'center',
						value: 'cinvccode',
						filterable: false,
						class: 'fixed'
					},
					{
						text: '存货大类名称',
						align: 'center',
						value: 'cinvcname',
						filterable: false,
						class: 'fixed'
					},
					{
						text: '单位',
						align: 'center',
						value: 'unitname',
						filterable: false,
						class: 'fixed'
					},
					{
						text: '供应商编码',
						align: 'center',
						value: 'cvencode',
						filterable: false,
						class: 'fixed'
					},
					{
						text: '启用日期',
						align: 'center',
						value: 'dsdate',
						filterable: false,
						class: 'fixed'
					},
					{
						text: '停用日期',
						align: 'center',
						value: 'dedate',
						filterable: false,
						class: 'fixed'
					},
					{
						text: '是否自制',
						align: 'center',
						value: 'bself',
						filterable: false,
						class: 'fixed'
					},
					{
						text: '是否采购',
						align: 'center',
						value: 'bPurchase',
						filterable: false,
						class: 'fixed'
					},
					{
						text: '是否在制',
						align: 'center',
						value: 'bproducing',
						filterable: false,
						class: 'fixed'
					}
				]
			},
		}
	}
</script>
